<?php

class TopicsController extends MController {
    public function showAction() {
        $params = $this->getRequest()->getGet();

        // Increment number of views
        $topic = Topic::find($params['t']);
        $topic->num_views++;
        $topic->save();

        $posts = Post::find('all', array(
                                        'conditions' => array('topic_id = ?', $params['t']),
                                        'order' => 'created_time DESC'));
        $this->render('show', array(
                                'posts'=>$posts,
                                'topic'=>$topic));
    }

    public function replyAction() {

        // User is connected ?
        if(MSession::getInstance()->IsLogged()) {

            $postDatas = $this->getRequest()->getPost();

            // Render the preview
            if ( $this->getRequest()->isAjax() ) {
                $this->renderPartial('postPreview', array('postDatas' => $postDatas));

            } else if ( $this->getRequest()->isPost() ) {

                $postDatas = $this->getRequest()->getPost();

                // Insert the post
                if( isset($postDatas['submit']) )
                {
                    try {
                        Post::create(array(
                                            'created_time' => date('Y-m-d H:i:s'),
                                            'poster' => MSession::getInstance()->getValue('username'),
                                            'poster_email' => MSession::getInstance()->getValue('userMail'),
                                            'poster_id' => MSession::getInstance()->getValue('userID'),
                                            'message' => $postDatas['message'],
                                            'topic_id' => $postDatas['tid']
                                            ));

                        // Increment number of replies
                        $topic = Topic::find($postDatas['tid']);
                        $topic->last_post_time = date('Y-m-d H:i:s');
                        $topic->num_replies++;
                        $topic->save();

                        // Increment number of posts and change the last post
                        $forum = Forum::find($topic->ml_forum_forum_id);
                        $forum->posts_number++;
                        $forum->last_post_id = Post::last()->id;
                        $forum->save();

                        __redirect(__route('ml-forum/topics/show&t='.$postDatas['tid']));
                    }
                    catch (Exception $e)
                    {
                        // TODO : Redirect with an error
                    }
                }
            }
            // Render the form
            else {
                $params = $this->getRequest()->getGet();
                $topic = Topic::find($params['t']);
                $posts = Post::find('all', array('conditions' => array('topic_id = ?', $params['t'])));

                $replyTopicForm = new ReplyTopicForm();
                $replyTopicForm->add(MForm::ELEMENT_HIDDEN, 'tid')->setValue($params['t']);

                $this->render('reply', array(
                                        'posts'=>$posts,
                                        'topic'=>$topic,
                                        'replyTopicForm'=>$replyTopicForm ));
            }
        }
        // User is not connected !
        else {
            __redirect(__route('ml-forum/home'));
        }
    }
    

    public function createAction() {

        // User is connected ?
        if(MSession::getInstance()->IsLogged()) {

            $postDatas = $this->getRequest()->getPost();

            // Render the preview
            if ( $this->getRequest()->isAjax() ) {
                $this->renderPartial('topicPreview', array('postDatas' => $postDatas));

            } else if( $this->getRequest()->isPost() ) {

                // Create the topic
                if( isset($postDatas['create']) )
                {

                    try {
                        Topic::create(array(
                                            'subject' => $postDatas['subject'],
                                            'created_time' => date('Y-m-d H:i:s'),
                                            'last_post_time' => date('Y-m-d H:i:s'),
                                            'last_post_user' => MSession::getInstance()->getValue('userID'),
                                            'ml_users_id' => MSession::getInstance()->getValue('userID'),
                                            'num_views' => '0',
                                            'num_replies' => '0',
                                            'closed' => '0',
                                            'sticky' => '0',
                                            'ml_forum_forum_id' => $postDatas['fid']
                                            ));
                        $lastTopicID = Topic::last()->id;

                        Post::create(array(
                                            'created_time' => date('Y-m-d H:i:s'),
                                            'poster' => MSession::getInstance()->getValue('username'),
                                            'poster_email' => MSession::getInstance()->getValue('userMail'),
                                            'poster_id' => MSession::getInstance()->getValue('userID'),
                                            'message' => $postDatas['first_post'],
                                            'topic_id' => $lastTopicID
                                            ));


                        // Increment number of topics
                        $forum = Forum::find($postDatas['fid']);
                        $forum->last_post_id = Post::last()->id;
                        $forum->topics_number++;
                        $forum->posts_number++;
                        $forum->save();

                    }
                    catch (Exception $e) {
                        // TODO : Redirect with an error
                    }

                    MRequest::redirect(__route('ml-forum/topics/show&t='.$lastTopicID));
                }
            }
            // Render the form
            else
            {
                $getDatas = $this->getRequest()->getGet();
                $forum = Forum::find($getDatas['f']);

                $createTopicForm = new CreateTopicForm();
                $createTopicForm->add(MForm::ELEMENT_HIDDEN, 'fid')->setValue($getDatas['f']);
                $this->render('create', array(
                                        'forum' => $forum,
                                        'createTopicForm' => $createTopicForm ));
            }
        }
        // User is not connected !
        else {
            __redirect(__route('ml-forum/home'));
        }
    }


    public function modifyAction() {

        // User is connected ?
        if(MSession::getInstance()->IsLogged()) {
            // Render the preview
            if ( $this->getRequest()->isAjax() ) {
                $postDatas = $this->getRequest()->getPost();
                $modifyPostForm = new ModifyPostForm();

                $this->renderPartial('postModify', array(
                                                        'postDatas' => $postDatas,
                                                        'modifyPostForm' => $modifyPostForm));
            }   
        }
        // User is not connected !
        else {
            __redirect(__route('ml-forum/home'));
        }

    }

}

?>